import 'package:flutter/material.dart';
import 'package:flutter_blog/constants/constants.dart';
import 'package:flutter_blog/controller/MenuController.dart';
import 'package:flutter_blog/model/Blog.dart';
import 'package:flutter_blog/page/home/components/responsive.dart';
import 'package:get/get.dart';
import 'package:logger/logger.dart';

import 'components/BlogPost.dart';
import 'components/categories.dart';
import 'components/recent_posts.dart';
import 'components/search.dart';

class HomeScreen extends StatelessWidget {
  HomeScreen({
    Key? key,
  }) : super(key: key);

  final MenuController _menuController = Get.put(MenuController());

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Expanded(
          flex: 2,
          child: Obx(() {
            return buildListView();
          }),
        ),
        if (!Responsive.isMobile(context)) SizedBox(width: kDefaultPadding),
        // Sidebar
        if (!Responsive.isMobile(context))
          Expanded(
            child: Column(
              children: [
                Search(),
                SizedBox(height: kDefaultPadding),
                Categories(),
                SizedBox(height: kDefaultPadding),
                RecentPosts(),
              ],
            ),
          ),
      ],
    );
  }

  Widget buildListView() {
    //获取数据
    RxList list = _menuController.blogList;

    if (list.length == 0) {
      return Text("加载中 ... ");
    }
    return ListView.builder(
      //内边距
      padding: EdgeInsets.only(bottom: 22),
      //禁止滑动
      physics: NeverScrollableScrollPhysics(),
      //包裹
      shrinkWrap: true,
      //子条目个数
      itemCount: list.length,
      //每个子Widget
      itemBuilder: (BuildContext context, int index) {
        //获取对应的数据
        Blog bean = list[index];
        return BlogPostCard(blog: bean);
      },
    );
  }
}
